//
//  NSXFirstPageWEBViewController.m
//  B段项目
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 徐茂怀. All rights reserved.
//

#import "NSXFirstPageWEBViewController.h"
#import "FitHeader.h"
#import "MBProgressHUD.h"
#import "NSXFirstPageListModel.h"
#import "CoreDataManager.h"
#import "FoodDetailModel.h"

@interface NSXFirstPageWEBViewController ()<UIWebViewDelegate>
@property(nonatomic , retain)UIWebView *webView;
@property(nonatomic , retain)MBProgressHUD *progressView;
@property(nonatomic,strong)CoreDataManager *manager;
@property(nonatomic,strong)NSArray *dataArray;
@property(nonatomic,assign)BOOL isCollection;
@property(nonatomic,strong)UIAlertController *alertController;
@end

@implementation NSXFirstPageWEBViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = _modelString;
    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];


    
    //webView部分
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, -60 *FitHeight, 375 *FitWidth, 870 *FitHeight)];
    _webView.delegate = self;
    NSString *urlString = [NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.detail.php?id=%@" ,_idString];
    //关闭webView的回弹效果
    [(UIScrollView *)[[_webView subviews] objectAtIndex:0] setBounces:NO];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    [_webView loadRequest:request];
    [self.view addSubview:_webView];
    
    //加载菊花
    self.progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"请稍等...";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor colorWithRed:0.58 green:0.6 blue:0.61 alpha:0.3];
    [self.view addSubview:_progressView];
    // Do any additional setup after loading the view.
}

-(void)loadView
{
    [super loadView];
    _isCollection = NO;
    _manager = [CoreDataManager shareManager];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"shoucang"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonAction)];
    
    
    if ([self selectAction].count != 0) {
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"yishoucang"]];
        _isCollection = YES;
    }
}

-(void)addAction
{
    FoodDetailModel *model = [NSEntityDescription insertNewObjectForEntityForName:@"FoodDetailModel" inManagedObjectContext:_manager.managedObjectContext];
    model.age = _NSXModel.age;
    model.category = _NSXModel.category;
    model.effect = _NSXModel.effect1;
    model.iD = _NSXModel.ID;
    model.title = _NSXModel.title;
    model.thumb = _NSXModel.thumb;
    [_manager saveContext];
}

-(void)deleteAction
{
    FoodDetailModel *model = [[self selectAction] lastObject];
    [_manager.managedObjectContext deleteObject:model];
    [_manager saveContext];
}

-(NSArray *)selectAction
{
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"FoodDetailModel"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"iD = %@", _NSXModel.ID];
    [fetchRequest setPredicate:predicate];
    
    _dataArray = [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    return _dataArray;
}

-(void)rightBarButtonAction
{
    if (_isCollection) {
        [self deleteAction];
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"shoucang"]];
        _alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"取消收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:NO];
         UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [self presentViewController:_alertController animated:YES completion:nil];
        [_alertController addAction:alertAction];
        _isCollection = NO;
    }else {
        [self addAction];
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"yishoucang"]];
        _alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:NO];
        [_alertController addAction:alertAction];
        [self presentViewController:_alertController animated:YES completion:nil];
        _isCollection = YES;
    }
}

-(void)timerAction:(NSTimer *)timer
{
    [_alertController dismissViewControllerAnimated:YES completion:nil];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_progressView removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
